package APong.World;

import java.util.ArrayList;
import java.util.Random;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.StateBasedGame;

import APong.GameManager;
import APong.Entity.Ball;
import APong.Entity.Entity;
import APong.Entity.Paddle;
import APong.Entity.ScoreZone;
import APong.Entity.Wall;

public class World {
	//normal world vars
	public static Random random = new Random();
	protected int deltaTime;
	public ArrayList<Entity> entities = new ArrayList<>();
	public int Player1, Player2;
	
	
	//added gameplay vars
	public int WallSize = 30;
	public Wall TopWall;
	public Wall BottomWall;
	public Wall LeftWall;
	public Ball ball;
	public Paddle player;
	public ScoreZone ScoreWall;
	public void addEntity(Entity e){
		e.setRemoved(false);
		entities.add(e);
	}
	public void init(GameContainer gc){
		//wall
		TopWall = new Wall(this,0,-(WallSize - 1), gc.getWidth(),WallSize);
		this.addEntity(TopWall);
		
		BottomWall = new Wall(this,0,gc.getHeight() - (1), gc.getWidth(),WallSize);
		this.addEntity(BottomWall);
		
		LeftWall = new Wall(this,gc.getWidth() - 1, 0, WallSize, gc.getWidth());
		LeftWall.setIsHorizontal(false);
		this.addEntity(LeftWall);
		
		ScoreWall = new ScoreZone(this,-(WallSize - 1),0, WallSize, gc.getWidth());
		this.addEntity(ScoreWall);
		//ball
		ball = new Ball(this,gc.getWidth() / 2, gc.getHeight() / 2);
		ball.setYVel(.4f);
		ball.setXVel(.4f);
		this.addEntity(ball);
		
		//paddle
		player = new Paddle(this,0,250);
		player.setIsPlayer(true);
		this.addEntity(player);
	}
	public void render(GameContainer gc, Graphics g){
		g.drawString("Player1: " + Player1, 100,100);
		for(Entity e: entities){
			e.render(gc, g);
		}
	}
	public void update(GameContainer gc, StateBasedGame sbg, int delta){
	//	if(delta <= 10){
		this.deltaTime = delta;
		//}else{
		//	deltaTime = 10;
		//}
		for(Entity e: entities){
			if(e.isRemoved()){
				entities.remove(e);
				return;
			}
			e.update(gc, sbg);
		}
		
		
		
	}
	
	//setters
	
	//getters
	public int getDeltaTime(){
		return deltaTime;
	}
	public ArrayList<Entity> getEntities(Shape shape){
		ArrayList<Entity> result = new ArrayList<>();
		for(Entity e:entities){
			if(shape.intersects(e.getCollisionShape(e.getXVel(), e.getYVel()))){
				result.add(e);
			}
		}
		
		return result;
	}
}
